package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2017/8/21 0021.
 */
@Controller
@RequestMapping(ControllerConstants.GOODS)

public class GoodsMCtronller extends BaseMobileController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询特价商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getSpecialGoods")
    public void getSpecialGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int cityId = getInt(json, "cityId", 0);
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 10);
                String uLng = getString(json, "uLng", "");
                String uLat = getString(json, "uLat", "");
                return goodsService.getSpecialGoods(cityId, nowPage, pageSize, uLng, uLat);
            }
        });
    }

    /**
     * 通过关键字查询商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getGoodsByKey")
    public void getGoodsByKey(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int cityId = getInt(json, "cityId", 0);
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 20);
                String key = getString(json, "key", "");
                return goodsService.getGoodsByKey(nowPage, pageSize, cityId, key);
            }
        });
    }

    /**
     * 每个商品分类下的商品列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getGoodsByBussAndTypeId")
    public void getGoodsByBussAndTypeID(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int companyId = getInt(json, "bussId", 0);
                int userId = getInt(json, "userId", 0);
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 20);
                return goodsService.getGoodsByBussAndTypeID(nowPage, pageSize, companyId, userId);
            }
        });
    }

    /**
     * 获取商品详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getGoodsById")
    public void getGoodsById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int goodsId = getInt(json, "goodsId", 0);
                int userId = getInt(json, "userId", 0);
                double ulng = getDouble(json, "uLng", 0);
                double ulat = getDouble(json, "uLat", 0);
                return goodsService.getGoodsDetails(goodsId, ulng, ulat, userId);
            }
        });
    }

    /**
     * 获取装备商城商品详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findEquipmentDetails")
    public void findEquipmentDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int goodsId = getInt(json, "goodsId", 0);
                int userId = getInt(json, "userId", 0);
                double ulng = getDouble(json, "uLng", 0D);
                double ulat = getDouble(json, "uLat", 0D);
                return goodsService.getGoodsDetails(goodsId, ulng, ulat, userId);
            }
        });
    }

    /**
     * 查询装备列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findEquipmentList")
    public void findEquipmentList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int companyTypeId = getInt(json, "companyTypeId", 57);
                int userId = getInt(json, "userId", 0);
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 20);

                return goodsService.findEquipmentList(companyTypeId, nowPage, pageSize, userId);
            }
        });
    }

    /**
     * 查询装备商城商家信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findEquipmentInfo")
    public void findEquipmentInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int companyTypeId = getInt(json, "companyTypeId", 57);
                int userId = getInt(json, "userId", 0);
                double ulng = getDouble(json, "ulng", 0);
                double ulat = getDouble(json, "ulat", 0);
                return goodsService.findEquipmentInfo(companyTypeId, ulat, ulng, userId);
            }
        });
    }

}
